//
//  SRSilenceClockViewController.m
//  SportRing
//
//  Created by icelee on 14-11-1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRSilenceClockViewController.h"
#import "SRSettingClockViewController.h"
#import "SRDB.h"
#import "SRSilenceClockViewController(Additionals).h"
#import "SRAlarmService.h"

@interface SRSilenceClockViewController ()<SRSilenceClockViewDelegate>
{
    SRSettingClockViewController *next ;
}
@property (nonatomic, weak)IBOutlet SRSilenceClockView *srSilenceClockView;
@property (strong, nonatomic) IBOutlet UIButton *bundBtn;

@end

@implementation SRSilenceClockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCustomTitle:NSLocalizedString(@"智能闹钟", nil)];
    [self addBackButton];
   
    @weakify(self);
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:NEW_ALARM_REFRESH_NOTIFY
                                                          object:nil]
      takeUntil:self.rac_willDeallocSignal]
     subscribeNext:^(id x) {
         [self_weak_ setupData];
     }];
    [self.view bringSubviewToFront:_bundBtn];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self hideTabbar];
    self.navigationController.navigationBarHidden = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    SRMe *me = [SRMe currentUser];
    SRDevice * device = me.device;
    if (!device) {
        _bundBtn.hidden = NO;
    }else
        _bundBtn.hidden = YES;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self setupData];
}

- (void)setupData
{
    NSMutableArray * alarmsArr = [@[] mutableCopy];
    
#if TARGET_IPHONE_SIMULATOR
    SRAlarm * alarm0 = [SRAlarm findAlarmCINEForMe:[SRMe currentUser] alarmId:0];
    [alarm0 configAttributesWithBlock:^(SRAlarm *aSelf) {
        aSelf.on = @(YES);
        aSelf.monday = @(YES);
        aSelf.tuesday = @(YES);
        aSelf.time = @(800);
    }];
    SRAlarm * alarm1 = [SRAlarm findAlarmCINEForMe:[SRMe currentUser] alarmId:1];
    [alarm1 configAttributesWithBlock:^(SRAlarm *aSelf) {
        aSelf.on = @(YES);
        aSelf.sunday = @(YES);
        aSelf.monday = @(YES);
        aSelf.tuesday = @(YES);
        aSelf.wednesday = @(YES);
        aSelf.thursday = @(YES);
        aSelf.friday = @(YES);
        aSelf.saturday = @(YES);
        aSelf.time = @(1400);
    }];
    NSArray * alarms = @[alarm0, alarm1];
#elif TARGET_OS_IPHONE
    SRAlarm * alarm0 = [SRAlarm findAlarmCINEForMe:[SRMe currentUser] alarmId:0];
    SRAlarm * alarm1 = [SRAlarm findAlarmCINEForMe:[SRMe currentUser] alarmId:1];
    NSArray * alarms = @[alarm0, alarm1];
#endif
    @weakify(self);
    
    [alarms enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        SRAlarm * alarm = obj;
        
        SRSilenceClockTableViewCellModel *model = [[SRSilenceClockTableViewCellModel alloc] init];
        
        CLOCK_RING_TYPE clock_type;
        
        NSString *clock_time = [NSString new];
        
        NSMutableArray *weekDays = [NSMutableArray new];
        
        [self_weak_ getAlarmSettingDetailsFromAlarm:alarm alarmType:&clock_type alarmTime:&clock_time alarmWeekDays:&weekDays];
        
        NSMutableString *weekDaysString  = [NSMutableString new];
        
        [weekDays enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            NSString *week = obj;
            
            [weekDaysString appendString:[NSString stringWithFormat:@"%@ ",week]];
        }];
        
        model.alarmId = alarm.alarmId;
        
        model.time = clock_time;
        
        model.monday = alarm.monday;
        model.tuesday = alarm.tuesday;
        model.wednesday = alarm.wednesday;
        model.thursday = alarm.thursday;
        model.friday = alarm.friday;
        model.saturday = alarm.saturday;
        model.sunday = alarm.sunday;

        
        model.ringType = clock_type;
        
        model.openSwitch = alarm.on.boolValue;
        
        [alarmsArr addObject:model];
        
    }];
    
    [_srSilenceClockView setClocks:alarmsArr];
}


#pragma  mark - SRSilenceClockViewDelegate

- (void)SRSilenceClockView:(SRSilenceClockView *)srSilenceClockView srSilenceClockTableViewCellModel:(SRSilenceClockTableViewCellModel*)model
{
    [SVProgressHUD show];
    
    [[SRAlarmService updateAlarmToDevice:[SRSilenceClockTableViewCellModel toSRAlarm:model]] subscribeNext:^(id x) {
        
        [SVProgressHUD dismiss];
        
    } error:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"闹钟设置失败"];
        
    }];
}

- (void)didSelectedClock:(SRSilenceClockTableViewCellModel *)model
{
    UIStoryboard *SportRingMainBoard = [UIStoryboard storyboardWithName:[SRSourceConfig uiMainStoryBoard] bundle:nil];
    next = [SportRingMainBoard instantiateViewControllerWithIdentifier:@"SRSettingClockViewController"];
    next.clockModel = model;
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:next];
//    [self presentViewController:nav animated:YES completion:nil];
    next.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    next.srSettingClockView.backgroundColor = [UIColor clearColor];
    next.view.center = CGPointMake(next.view.center.x, next.view.center.y - 64);
    [self.view addSubview:next.view];
    [next.canCelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [next.okBtn addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)cancelClick
{
    [next onLeftButtonPressed];
}

- (void)okClick
{
    [next onRightButtonPressed];
}

- (void)updateAlarmData
{
    @weakify(self);
    
    [[SRAlarmService readAlarmsFromDevice] subscribeNext:^(id x) {
        
        [self_weak_ setupData];
        
        [_srSilenceClockView stopAnimating];
    }];
}

- (IBAction)totrunk:(UIButton *)sender {
     [self performSegueWithIdentifier:@"pushtoMytrunk" sender:sender];
}

@end
